package com.sc.nft.dao;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.DigitalCollectionProductInfoSubscribe;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.SubscribeWinStatusEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.DigitalCollectionProductInfoSubscribeWinBallotMapper;
import com.sc.nft.entity.DigitalCollectionProductInfoSubscribeWinBallot;
import com.sc.nft.entity.dto.DigitalCollectionProductInfoSubscribeCodeRequest;
import com.sc.nft.mapper.DigitalCollectionProductInfoSubscribeWinBallotMapper;
import com.sc.nft.service.DigitalCollectionProductInfoSubscribeWinBallotService;
import com.sc.nft.vo.WinNoticePeopleVo;
import org.springframework.boot.actuate.integration.IntegrationGraphEndpoint;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户申购中签表(DigitalCollectionProductInfoSubscribeWinBallot)表Dao层
 *
 * @author 王伟成
 * @since 2024-05-15 18:09:08
 */
@Slf4j
@Repository
public class DigitalCollectionProductInfoSubscribeWinBallotDao extends ServiceImpl<DigitalCollectionProductInfoSubscribeWinBallotMapper, DigitalCollectionProductInfoSubscribeWinBallot> {


    /**
     * 获取需要发送短信的申购活动
     * @return
     */
    public List<DigitalCollectionProductInfoDrawVo> getResultAnnounce() {
        return baseMapper.getResultAnnounce();
    }

    /**
     * 该功能用来获取当前正常状态的用户申购码.
     * @param productId
     * @param userId
     * @return
     */
    public Integer getProductIdAndUserIdByCount(Long productId,Long userId){
        LambdaQueryWrapper<DigitalCollectionProductInfoSubscribeWinBallot> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getProductId,productId);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getUserId,userId);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getStatus, SubscribeWinStatusEnum.NORMAL);
        return count(wrapper);
    }

    public Integer getByProductIdAndUserIdAndWinStatus(Long productId,Long userId,SubscribeWinStatusEnum... subscribeWinStatusEnum){
        LambdaQueryWrapper<DigitalCollectionProductInfoSubscribeWinBallot> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getProductId,productId);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getUserId,userId);
        wrapper.in(subscribeWinStatusEnum!=null&&subscribeWinStatusEnum.length>0,DigitalCollectionProductInfoSubscribeWinBallot::getStatus, subscribeWinStatusEnum);
        return count(wrapper);
    }

    /**
     * 用户购买时候锁单申购码冻结.
     * @param productId
     * @param userId
     * @param sum
     * @param orderId
     * @return
     */
    public Boolean freezeTheSubscriptionCode(Long productId,Long userId,Integer sum,Long orderId) {
        LambdaUpdateWrapper<DigitalCollectionProductInfoSubscribeWinBallot> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getProductId, productId);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getUserId, userId);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getStatus, SubscribeWinStatusEnum.NORMAL);
        wrapper.set(DigitalCollectionProductInfoSubscribeWinBallot::getOrderId, orderId);
        wrapper.set(DigitalCollectionProductInfoSubscribeWinBallot::getStatus, SubscribeWinStatusEnum.FROZEN);
        wrapper.last("limit " + sum);
        int update = baseMapper.update(new DigitalCollectionProductInfoSubscribeWinBallot(), wrapper);
        if (sum!=update) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
    public Boolean updateTheSubscriptionCode(Long productId, Long userId, Long order) {
        LambdaUpdateWrapper<DigitalCollectionProductInfoSubscribeWinBallot> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getProductId, productId);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getUserId, userId);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getOrderId, order);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getStatus, SubscribeWinStatusEnum.FROZEN);
        wrapper.set(DigitalCollectionProductInfoSubscribeWinBallot::getStatus, SubscribeWinStatusEnum.USE);
        return update(new DigitalCollectionProductInfoSubscribeWinBallot(), wrapper);
    }
    /**
     * 释放申购码
     * @param productId
     * @param userId
     * @param orderId
     * @return
     */
    public boolean releaseApplicationCode(Long productId, Long userId, Long orderId) {
        LambdaUpdateWrapper<DigitalCollectionProductInfoSubscribeWinBallot> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getProductId, productId);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getUserId, userId);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getOrderId, orderId);
        wrapper.eq(DigitalCollectionProductInfoSubscribeWinBallot::getStatus, SubscribeWinStatusEnum.FROZEN);
        wrapper.set(DigitalCollectionProductInfoSubscribeWinBallot::getStatus, SubscribeWinStatusEnum.NORMAL);
        return update(new DigitalCollectionProductInfoSubscribeWinBallot(), wrapper);
    }

    /**
     * 中签用户列表分页
     * @param request
     * @return
     */
    public Page<WinningUsersVO> getSubscribeCodeUserPage(DigitalCollectionProductInfoSubscribeCodeRequest request) {
        return baseMapper.getSubscribeCodeUserPage(request.toMp(),request.getId(),request.getUserTel(),request.getStatus());
    }

    /**
     * 获取申购列表汇总数据
     * @param id
     * @return
     */
    public DigitalCollectionProductInfoSubscribeCodeSummaryVO getSubscribeCodeSummary(Long id) {
        return baseMapper.getSubscribeCodeSummary(id);
    }

    /**
     * 获取需要发短信的中签用户
     * @param productId
     * @return
     */
    public List<WinNoticePeopleVo> getByUserTelAndStatus(Long productId) {
        return baseMapper.getByUserTelAndStatus(productId);
    }

    /**
     * 同步更新发送完短信字段notice_status
     * @param id
     */
    public void updateStatusById(Long id) {
        baseMapper.updateStatusById(id);
    }
    public Page<MySubscribeCodesWinDataVo> getSubscribeWin(Page page, Long productId) {
        return baseMapper.getSubscribeWin(page,productId);
    }
}
